package eSchool.GUI.Components;

import eSchool.Application.State.DataState;
import eSchool.BL.EntidadeBase;
import eSchool.BL.Utilizador;
import eSchool.Utils.TextHelper;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class Login extends JFrame {

    private JLabel lblUser;
    private JLabel lblPassword;
    private JTextField txtUser;
    private JPasswordField txtPassword;
    private JButton btnOk;
    private JPanel contentPane;
    private int tentativas;

    public Login() {
        super();
        create();
        this.setVisible(true);
        tentativas = 3;
    }

    private void create() {
        lblUser = new JLabel();
        lblPassword = new JLabel();
        txtUser = new JTextField();
        txtPassword = new JPasswordField();
        btnOk = new JButton();
        contentPane = (JPanel) this.getContentPane();


        lblUser.setHorizontalAlignment(SwingConstants.LEFT);
        lblUser.setText("Utilizador");

        lblPassword.setHorizontalAlignment(SwingConstants.LEFT);
        lblPassword.setText("Password");

        txtUser.setToolTipText("Nome de utilizador");
        txtUser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                txtUser_actionPerformed(e);
            }
        });

        txtPassword.setToolTipText("Inserir password");
        txtPassword.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                txtPassword_actionPerformed(e);
            }
        });

        btnOk.setText("Login");
        btnOk.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    btnOk_actionPerformed(e);
                } catch (Exception ex) {
                    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        contentPane.setLayout(null);
        contentPane.setBorder(BorderFactory.createEtchedBorder());
        //contentPane.setBackground(new Color(204, 204, 204));
        addComponent(contentPane, lblUser, 35, 10, 106, 18);
        addComponent(contentPane, lblPassword, 35, 47, 97, 18);
        addComponent(contentPane, txtUser, 110, 10, 183, 22);
        addComponent(contentPane, txtPassword, 110, 45, 183, 22);
        addComponent(contentPane, btnOk, 150, 75, 83, 28);

        this.setTitle("E-School Login");
        this.setLocation(new Point(76, 182));
        this.setSize(new Dimension(335, 141));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setResizable(false);
    }

    /** Add Component Without a Layout Manager (Absolute Positioning) */
    private void addComponent(Container container, Component c, int x, int y, int width, int height) {
        c.setBounds(x, y, width, height);
        container.add(c);
    }

    private void txtUser_actionPerformed(ActionEvent e) {
    }

    private void txtPassword_actionPerformed(ActionEvent e) {
    }

    private void btnOk_actionPerformed(ActionEvent e) throws Exception {
        String username = new String(txtUser.getText());
        String password = new String(txtPassword.getPassword());
        String textoErro = "Tem que inserir o nome de utilizador e a password. Tem mais ";
        tentativas--;
        
        if(tentativas>0){
            if (username.equals(TextHelper.EmptyString) || password.equals(TextHelper.EmptyString)) {
                btnOk.setEnabled(false);
                JLabel errorFields = new JLabel(textoErro + tentativas + " tentativa(s).");
                JOptionPane.showMessageDialog(null, errorFields);
                txtUser.setText(TextHelper.EmptyString);
                txtPassword.setText(TextHelper.EmptyString);
                btnOk.setEnabled(true);
                this.setVisible(true);
            } else {
                Utilizador utilizador = DataState.getConnection().logIn(username,password);
                if(utilizador != null && !utilizador.getCodUtilizador().equals(TextHelper.EmptyString)){
                    this.setVisible(false);
                    new Desktop().setVisible(true);
                }
                else{
                    JLabel errorFields = new JLabel(textoErro + tentativas + " tentativa(s).");
                    JOptionPane.showMessageDialog(null, errorFields);
                    txtUser.setText(TextHelper.EmptyString);
                    txtPassword.setText(TextHelper.EmptyString);
                }
            }
        }
        else{
            this.dispose();
        }
    }
    
}
